package com.szqhyxj.youxiangju.biz.merchantHome.shop.order;

import com.szqhyxj.youxiangju.biz.merchantHome.bean.FacilityList;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class OrderDetailsPresenter extends AbBasePresenter implements OrderDetailsContract.Presenter {

    private final OrderDetailsContract.View view;
    private OrderDeetails data;

    public OrderDetailsPresenter(OrderDetailsContract.View view){
        this.view = view;
    }


    @Override
    public void init(String orderId){
        addDisposables(OrderModel.getOrderDetail(orderId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
        .subscribe(new Consumer<BaseResult<OrderDeetails>>(){
            @Override
            public void accept(BaseResult<OrderDeetails> orderDeetailsBaseResult) throws Exception{
                view.dismissLoading();
                if(orderDeetailsBaseResult.businessLogicSuccess()){
                    data = orderDeetailsBaseResult.getData();
                    view.showOrderDetails(data);
                }else {
                    view.showToast(orderDeetailsBaseResult.getMessage());
                }
            }
        }, new Consumer<Throwable>(){
            @Override
            public void accept(Throwable throwable) throws Exception{
                view.showToast(throwable.getMessage());
                view.dismissLoading();
            }
        }));
    }

    @Override
    public void nowConfirm(){
        addDisposables(OrderModel.confirmReceipt(data.getPayOrderId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.showConfirmOrder();
                            view.showToast("确定收货成功");
                        }else{
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void initList(){
        addDisposables(OrderModel.getPosList(data.getPayOrderId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<List<FacilityList>>>(){
                    @Override
                    public void accept(BaseResult<List<FacilityList>> facilityListBaseResult) throws Exception{
                        view.dismissLoading();
                        if(facilityListBaseResult.businessLogicSuccess()){
                            view.showListPop(facilityListBaseResult.getData());
                        }else{
                            view.showToast(facilityListBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}